package cn.dazhou.wificonnectdemo

import android.Manifest
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import cn.dazhou.wificonnectdemo.databinding.ActivityMainBinding
import cn.dazhou.wificonnectdemo.wifi.WifiScanHandler
import cn.dazhou.wificonnectdemo.wifi.WifiAutoConnector
import cn.dazhou.wificonnectdemo.wifi.WifiUtil
import okhttp3.Call
import okhttp3.OkHttpClient
import okhttp3.Response
import java.io.IOException

//            val wifiName = "H30"
//            val wifiPwd = "99999999"
//            val wifiName = "4G-WiFi-A328"
//            val wifiPwd = "12345678"
val wifiName = "Mi 11 Pro"
val wifiPwd = "18520109899"


class MainActivity : AppCompatActivity() {

    private val binding: ActivityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        registerForActivityResult(ActivityResultContracts.RequestPermission()) { Timber.fi("授权结果：$it") }.launch(Manifest.permission.ACCESS_FINE_LOCATION)

        binding.scan.setOnClickListener { WifiScanHandler.startScan() }
        binding.connectWifi.setOnClickListener {
            WifiUtil.connectToWiFi(wifiName, wifiPwd)
        }
        binding.connect.setOnClickListener {
//            val url = "http://192.168.1.129:8080/hello/hello.json"
//            val url = "http://192.168.1.55:8081"
            val url = "https://jsonview.com/example.json"
            OkHttpClient.Builder().build().newCall(okhttp3.Request.Builder().url(url).build()).enqueue(object : okhttp3.Callback {
                override fun onFailure(call: Call, e: IOException) {
                    Timber.fi("网络连接失败：${e.message}")
                }

                override fun onResponse(call: Call, response: Response) {
                    Timber.fi("网络连接成功：${response.body?.string()}")
                }
            })
        }

        WifiAutoConnector.init()
    }




    override fun onDestroy() {
        super.onDestroy()
        WifiAutoConnector.unInit()
    }
}